package com.weixin.paleituo.fragment;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.weixin.paleituo.R;
import com.weixin.paleituo.activity.LoginActivity;
import com.weixin.paleituo.activity.MyMessageActivity;
import com.weixin.paleituo.adapter.HomeHouseRecycleAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BaseFragment;
import com.weixin.paleituo.bean.HomeHouseBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.eventbus.SoldOutEvent;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.ClassifiedRecyclerView;
import com.weixin.paleituo.view.IndexBar;
import com.weixin.paleituo.view.LoadingPagerHead;
import com.weixin.paleituo.view.RecyclerViewDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;

/**
 * Author yichao
 * Time  2017/11/30 9:10
 * Dest  楼盘
 */

public class HouseFragment extends BaseFragment {


    @BindView(R.id.ll_headroot_house_fragment)
    LinearLayout mLlHeadrootHouseFragment;
    @BindView(R.id.iv_message_house_fragment)
    ImageView mIvMessageHouseFragment;
    @BindView(R.id.recycler_view)
    ClassifiedRecyclerView mRecyclerView;
    @BindView(R.id.index_bar)
    IndexBar mIndexBar;
    private LoadingPagerHead mLoadingPagerHead;

    private   List<HomeHouseBean> mHomeHouseBeans=new ArrayList<>();
    private HomeHouseRecycleAdapter mHomeHouseRecycleAdapter;


    @Override
    public View initView(Context context) {
        mLoadingPagerHead = new LoadingPagerHead(mContext) {
            @Override
            protected void close() {

            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(mContext, R.layout.house_fragment);
            }

            @Override
            protected void reLoading() {
                retry();

            }
        };
      //  UIUtils.inflate(mContext, R.layout.house_fragment);
        return mLoadingPagerHead;
    }

    /**
     * 重试
     */
    private void retry() {
        Observable<StatusCode<List<HomeHouseBean>>> cityHouseListObservable = ApiUtils.getApi().getCityHouseList(Contans.defaultToken);
        HttpUtil.getInstance().toSubscribe(cityHouseListObservable, new ProgressSubscriber<List<HomeHouseBean>>(mContext) {
            @Override
            protected void _onNext(StatusCode<List<HomeHouseBean>> listStatusCode) {
                List<HomeHouseBean> data = listStatusCode.getData();

                if (listStatusCode.isFlag() && data != null && data.size() > 0) {
                    mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                    Collections.sort(data);
                    mRecyclerView.setVisibility(View.VISIBLE);
                    mIndexBar.setVisibility(View.VISIBLE);
                    mHomeHouseBeans.clear();
                    mHomeHouseBeans.addAll(data);
                    mHomeHouseRecycleAdapter.notifyDataSetChanged();

                  /*  mRecyclerView.addItemDecoration(new RecyclerViewDecoration(mContext, data));
                    mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
                    mRecyclerView.setAdapter(new HomeHouseRecycleAdapter(mContext, data));*/

                } else {
                    mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                }
            }

            @Override
            protected void _onError(String message) {
                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                ToastUtils.makeText(message + "");

            }
        }, "", lifecycleSubject, false, true);


    }

    @Override
    protected void initData() {
        super.initData();
        EventBus.getDefault().register(this);
 /*       if (Build.VERSION.SDK_INT >= 21) {
            mLlHeadrootHouseFragment.setPadding(0, UIUtils.dip2px(20), 0, 0);
            mLlHeadrootHouseFragment.setBackgroundColor(UIUtils.getColor(R.color.c_29ce7e));

        } else {
            mLlHeadrootHouseFragment.setPadding(0, 0, 0, 0);
        }*/
        mHomeHouseBeans.clear();
        mHomeHouseRecycleAdapter = new HomeHouseRecycleAdapter(mContext, mHomeHouseBeans);
        mRecyclerView.addItemDecoration(new RecyclerViewDecoration(mContext, mHomeHouseBeans));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(mHomeHouseRecycleAdapter);
 

        retry();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
public void sokeoutEvent(SoldOutEvent soldOutEvent){
        int mtype = soldOutEvent.getMtype();
        if (mtype==Contans.HOUSE_SOLDOUT_TYPE){

            mLoadingPagerHead.showPagerView(Contans.STATE_LOADING);
            retry();
        }

}


    @OnClick(R.id.iv_message_house_fragment)
    public void onViewClicked() {
        if (BaseApplication.isLoginSuccess){
            MyMessageActivity.startActivity(mContext,BaseApplication.getUserId());
        }else {
            LoginActivity.startActivity(mContext,true);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
